package com.itheima.stock.service.impl;

import com.google.common.base.Strings;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.pojo.SysUser;
import com.itheima.stock.service.UserService;
import com.itheima.stock.utils.IdWorker;
import com.itheima.stock.vo.req.LoginReqVo;
import com.itheima.stock.vo.resp.LoginRespVo;
import com.itheima.stock.vo.resp.R;
import com.itheima.stock.vo.resp.ResponseCode;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public R<LoginRespVo> login(LoginReqVo vo) {
        //1.判断vo是否为空或用户名是否存在或密码是否存在
        if (vo==null|| Strings.isNullOrEmpty(vo.getUsername())||Strings.isNullOrEmpty(vo.getPassword())
        || Strings.isNullOrEmpty(vo.getCode()) || Strings.isNullOrEmpty(vo.getRkey()))
        {
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
        // 判断验证码是否正确
        String redisCode = (String) redisTemplate.opsForValue().get(vo.getRkey());
        // 比对验证码
        if (redisCode==null||!redisCode.equals(vo.getCode())) {
            redisTemplate.delete(vo.getRkey());
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
        //腾出redis的内存空间
        redisTemplate.delete(vo.getRkey());
        //2.判断该用户名是否存在
        SysUser userInfo = sysUserMapper.findUserInfoByUserName(vo.getUsername());
        if (userInfo==null) {
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }

        //3.判断密码是否正确
            //3.1密码不存在
        if (!passwordEncoder.matches(vo.getPassword(),userInfo.getPassword())) {
            return R.error(ResponseCode.SYSTEM_PASSWORD_ERROR.getMessage());
        }
            //3.2密码存在
        LoginRespVo respVo = new LoginRespVo();
        BeanUtils.copyProperties(userInfo,respVo);

        return R.ok(respVo);
    }

    @Override
    public R<Map> genCaptchaCode() {
        //1.生成长度为 4 的随机校验码
        String checkCode = RandomStringUtils.randomNumeric(4);
        //2.生成一个类似sessionid 的id 作为key,校验码为value保存在redis下
        String sessionId = String.valueOf(idWorker.nextId());
        redisTemplate.opsForValue().set(sessionId,checkCode,60, TimeUnit.SECONDS);
        //3.组装响应的Map对象
        HashMap<String, String> map = new HashMap<>();
        map.put("code",checkCode);
        map.put("rkey",sessionId);
        //4.返回组装数据
        return R.ok(map);
    }











}
